package mo.tags.stack;

import java.util.LinkedList;

public class L71 {

    public String simplifyPath(String path) {
        String[] names = path.split("/");
        LinkedList<String> queue = new LinkedList<>();
        for (String name : names) {
            if ("..".equals(name)) {
                if (!queue.isEmpty()) {
                    queue.removeLast();
                }
            } else if (name.length() > 0 && !".".equals(name)) {
                queue.add(name);
            }
        }
        StringBuilder builder = new StringBuilder();
        if (queue.isEmpty()) {
            builder.append("/");
        } else {
            while (!queue.isEmpty()) {
                builder.append("/");
                builder.append(queue.removeFirst());
            }
        }
        return builder.toString();
    }

    public static void main(String[] args) {
        L71 test = new L71();
        System.out.println(test.simplifyPath("/home//foo/"));
    }

}
